<script setup lang="ts">
import { User, Lock } from '@element-plus/icons-vue'
import { ref } from 'vue'
import { useUserStore } from '@/stores'
import { useRouter } from 'vue-router'
import { useRoute } from 'vue-router'
import { getTime } from '@/utils/time'

const userStore = useUserStore()
const route = useRoute()
const router = useRouter()
// 表单校验数据
const formModel = ref({
  username: 'admin',
  password: '111111'
})
// 自定义校验规则
const checkUsername = (rule: any, value: string, callback: any) => {
  // 用户名不能有空格
  if (value.indexOf(' ') !== -1) {
    callback(new Error('用户名不能有空格'))
  } else {
    callback()
  }
}
// 表单校验内容
const rules = ref({
  username: [
    {
      required: true,
      message: '请输入用户名',
      trigger: 'blur'
    },
    {
      min: 5,
      max: 10,
      message: '用户名长度在 5 到 10 个字符',
      trigger: 'blur'
    },
    {
      validator: checkUsername,
      trigger: 'blur'
    }
  ],
  password: [
    {
      required: true,
      message: '请输入密码',
      trigger: 'blur'
    },
    {
      pattern: /^[a-zA-Z0-9]{6,15}$/,
      message: '密码长度在 6 到 15 个字符，不能包含空格',
      trigger: 'blur'
    }
  ]
})
// 加载
const loading = ref(false)
// 预校验
const formRef = ref()
// 登录
const onLogin = async () => {
  await formRef.value.validate()
  // 登录
  loading.value = true
  try {
    //await userStore.goLogin(formModel.value)
    await userStore.goLogin(formModel.value)
    router.push({
      path: (route.query.redirect as string) || '/'
    })
    ElNotification({
      title: `Hi, ${getTime()}`,
      message: '欢迎回来',
      type: 'success'
    })
  } catch (error) {
    ElNotification({
      title: '登录失败',
      message: (error as Error).message,
      type: 'error'
    })
  }
  loading.value = false
}
</script>

<template>
  <div class="login_container">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <el-form
          class="login_form"
          :model="formModel"
          :rules="rules"
          ref="formRef"
          status-icon
        >
          <h1>Hello</h1>
          <h2>欢迎来到硅谷甄选</h2>
          <el-form-item prop="username">
            <el-input
              :prefix-icon="User"
              v-model.trim()="formModel.username"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              :prefix-icon="Lock"
              type="password"
              show-password
              v-model.trim()="formModel.password"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              @click="onLogin"
              :loading="loading"
              class="login_btn"
              type="primary"
              >登录</el-button
            >
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<style lang="scss" scoped>
.login_container {
  height: 100vh;
  width: 100vw;
  background: url('@/assets/images/background.jpg') no-repeat center/cover;
  .login_form {
    margin-top: 30vh;
    padding: 2vh 5vw;
    background: url('@/assets/images/login_form.png') no-repeat center/cover;
    h1 {
      font-size: 40px;
      color: #fff;
    }
    h2 {
      margin: 2vh 0;
      font-size: 20px;
      color: #fff;
    }
    .login_btn {
      width: 100%;
    }
  }
}
</style>
